#!/usr/bin/env python

import subprocess


# These are remote branches that got rebased or something, but I don't have
# control over.
ignored_hashes = set('''
    078daa1148a84849ea0890b388353acd7333fcea
    0832d2bdaa048fca1393f3d3810b8b8810535f9f
    08e86996736d77df7d694d0a67a126fc7eac7e94
    097631535fa4cdb87204980541d3f9bb9c7a9ffb
    16ce34ba1d1f1ed4327326a10565f1a9f07107b0
    183cf1447571aa48baf0665d17d41be1e48f2cc6
    245a4dca69cf0fff674c6ad5bcfb7929c7347bf6
    28b4b3988981471ff173679db3643418ff3f5aaa
    3977b5d5be22922f2eb4288e2682f1de8fad8e12
    5b9f192165855942918f9bd957c30e918b97cbeb
    6091e89de0ae4aff2ba76d21b1110409ef174b78
    636afe3f0b5b07233edae8e333db35c044c36b30
    74f79ef74ec281f5e0da51bcfd0b1051aa53edbf
    7737ef6e9e7307c40f326e61cc9291047540bc49
    8618940d333f44ff960d561dda34167d4dbb81d4
    a2fb55e97788809b5f33b1b0c9241fc77312f606
    aa044b3f62a6d7bf4dde18cba91b1d0dd8a0816a
    aa7d01ba458025ede1757e56b638002375bb864a
    aafe064e209b667f565c4f57a94b098474d0b184
    afac2d8f89673c012d1f4b845b006911f55d1d86
    b115786b950c87ef9c422752e014297903bca393
    b737c6ceb6750d00f62dfdaa40fee3e757c680a3
    b7bf427a485736e6e1c71605bdce101214bae09f
    ba02afa7ea160328b5a3be111c7e276fb9d3c961
    bc5ffe456345286a64ce33ffe5ce6a2ee8b63f40
    c45a337fe49875b1cc28a0501a704890be444765
    c6b1a5ac03e775ea46bffac7bbfea9d73cd03b87
    c9c0d63b09c450d494fba1c4073fbe18851dfaff
    cc270d6790c02e6c5e93313d1e6499ce534350b9
    cdd8e4c085a55e258bd551f7bcf4fee60474aa05
    eac71881c24d42f801e9c18e448855a402333960
    efd12926b1f446c32f5a239c0b2d351fa2d78101
    f04dce0e80b4f290482eba4fb3c3ec68f353bf01
    f0d1e82dee788085cf4afad7656a90966e40f7a0
    f518f6e7bf47e00fe0c73a5098ae40813920400f
    f779c4371fdace76ee572053b4acb3999ffd4107
'''.strip().split())


def get_branches(*args):
    cmd = ['git', 'branch', '-v', '--abbrev=40']
    cmd.extend(args)
    res = {}
    for line in subprocess.check_output(cmd).decode().splitlines():
        parts = line[2:].strip().split()
        name = parts[0]
        hash_ = parts[1]
        res[name] = hash_
    return res

def rm(*args):
    subprocess.check_call(('git', 'branch', '-D') + args)


# Clean up everything that was merged
for line in subprocess.check_output(['git', 'branch', '--merged']).decode().splitlines():
    line = line.strip()
    if not line:
        continue
    parts = line.split()
    if parts[0] == '*':
        continue
    if parts[-1] in ('develop', 'master'):
        continue
    rm(parts[-1])

for line in subprocess.check_output(['git', 'branch', '-r', '--merged']).decode().splitlines():
    name = line.strip().split()[-1]
    if not name:
        continue
    if name.split('/', 1)[-1] in ('develop', 'master'):
        continue
    rm('-r', name)


our_branches = get_branches()
for name, hash_ in get_branches('-r').items():

    if hash_ in ignored_hashes:
        print("Removing ignored", name)
        rm('-r', name)
        continue

    if name.startswith('origin/'):
        our_branches[name] = hash_


for name in get_branches('-r', '--merged'):
    if name.startswith('origin/'):
        continue
    print("Removing merged", name)
    rm('-r', name)

for name, hash_ in get_branches('-r', '--no-merged').items():

    remote, branch = name.split('/', 1)
    if remote == 'origin':
        continue

    for prefix in '', 'origin/':
        our_name = prefix + branch
        if our_branches.get(our_name) == hash_:
            print("Removing identical", name)
            rm('-r', name)
            break

# Anything that doesn't root at the same place as us.
for name in get_branches('-r', '--no-contains', 'e105c0b4e64a0471f3f5375a86342c33cb942e23'):
    rm('-r', name)



